package pers.cz.codegen.describe;

import pers.cz.codegen.enums.CodeStyleConfigItem;
import pers.cz.codegen.template.CommentTemplate;
import pers.cz.jefconfig.config.JefConfiguration;

/**
 * @program: jef-code-generator
 * @description: 常量描述
 * @author: Cheng Zhi
 * @create: 2022-08-07 16:08
 **/
public class JavaField extends AbstractJavaElement {

    private IClass returnType;
    public static final String descPlace = "##D";
    private String fieldComment = "/**\n" +
            "     *  " + descPlace + "\n" +
            "     */";
    public JavaField(int modifer, String name) {
        setModifer(modifer);
        setComment(name);
    }

    public JavaField(String name) {
        setName(name);
    }

    public void addReturnType(IClass iClass) {
        this.returnType = iClass;
    }

    @Override
    public void setComment(String myComment) {
        if (myComment == null) {
            return;
        }
        comment = fieldComment.replace(descPlace, myComment);
    }

    @Override
    public String toCode() {

        StringBuilder sb = new StringBuilder();
        sb.append(TAB_SYMBOL);
        sb.append(getComment());
        sb.append(LINE_SYMBOL);
        for (String annotation : this.annotations) {
            sb.append(TAB_SYMBOL);
            sb.append(annotation);
            sb.append(LINE_SYMBOL);
        }
        sb.append(TAB_SYMBOL);
        sb.append(getModifer());
        if (returnType == null) {
            sb.append("void ");
        } else {
            sb.append(returnType.getSimpleName());
            sb.append(SPACE_SYMBOL);
        }

        sb.append(getName());
        // 内容
        for (String content : getContents()) {
            sb.append(SPACE_SYMBOL);
            sb.append("=");
            sb.append(SPACE_SYMBOL);
            sb.append(content);
        }
        sb.append(SEMICOLON);
        sb.append(LINE_SYMBOL);

        return sb.toString();
    }
}
